// pages/userinfo/userinfo.js
var app = getApp();

var remainTime = 599

Page({

    /**
     * 页面的初始数据
     */
    data: {
        useravatar: undefined,
        username: undefined,
        role: undefined,
        showDeveloping: false,

        useTestData: false,
        countdown:'9:59'

    },
    changeDeveloping(e) {
        this.setData({
            showDeveloping: e.detail
        })
    },
    goToBloodPage: function () {
        wx.switchTab({
            url: '../bloodsugar/bloodsugar',
        })
    },
    goToFoodPage: function () {
        wx.switchTab({
            url: '../foodrecord/foodrecord',
        })
    },
    goToSportPage: function () {
        wx.switchTab({
            url: '../sportrecord/sportrecord',
        })
    },
    goToReportPage: function () {
        // this.setData({
        //     showDeveloping:true
        // })
        // return
        wx.navigateTo({
            url: '../reportdate/reportdate',
        })
    },
    goToEditPage: function () {
        wx.navigateTo({
            url: '../infoedit/infoedit',
        })
    },
    goToHelpPage: function () {
        this.setData({
            showDeveloping:true
        })
        return
    },
    // 退出登陆
    logout: function () {
        wx.clearStorage();
        wx.navigateTo({
            url: '../login/login',
        });
    },
    countdown(that){
        var time = remainTime
        var minute=Math.floor(time  / 60 );
        var second=time  % 60
        second<10?second='0'+second:'';
        that.setData({
            countdown : minute+':'+second
        })
        remainTime = time - 1
        if (remainTime < 0 || !that.data.useTestData) {
            that.setData({
                useTestData: false,
                countdown : '9:59'
            })
            remainTime = 599
        }
        if (that.data.useTestData) {
            setTimeout(that.countdown, 1000, that);
        }
    },
    openTest() {
        const url = 'https://ericwvi.site/bgmp/api/user?Action=SetUseTestData'
        let use = this.data.useTestData
        console.log(use)
        let that = this
        wx.request({
            url: url,
            method: 'POST',
            header: {
            "Content-Type": "application/json",
            "x-api-key": wx.getStorageSync('token')
            },
            data: {
            "enable": !use
            },
            success: (res) => {
                console.log(res.data);
                if (res.data.code === 200) {
                    that.setData({
                        useTestData: !use
                    })
                    if (!use) {
                        that.countdown(that)
                    }
                }
            },
            fail: () => {}
        });
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {},

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        console.log("OnShow")
        this.setData({
            useravatar: wx.getStorageSync('userInfo').avatar,
            username: wx.getStorageSync('userInfo').username,
            role: wx.getStorageSync('role'),
        })
        if (typeof this.getTabBar === 'function' && this.getTabBar()) {
            this.getTabBar().setData({
                selected: 3
            })
        }
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})